home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Apple Developer Connection Student Program
/
ADC Tools Sampler CD Disk 3 1999.iso
/
Documentation
/
Books
/
Learn Java on the Macintosh
/
Learn Java Projects
/
11.01 - components
/
UIApplet.java
< prev
next >
Wrap
Text File
|
1996-04-22
|
2KB
|
88 lines
/* -------------------------------------------------------------
This applet creates a few different user interface components
and detects when the user interacted with them.
Java's classes: Applet (applet)
System (lang)
Button (awt)
Choice (awt)
TextField (awt)
Checkbox (awt)
CheckboxGroup (awt)
Label (awt)
Event (awt)
Custom classes: UIApplet
------------------------------------------------------------- */
import java.awt.*;
public class UIApplet extends java.applet.Applet {
Button button;
Choice choice;
TextField textField;
/** Create a user interface. */
public void init() {
Checkbox checkbox;
CheckboxGroup checkboxGroup;
Label label;
// create a choice list
choice = new Choice();
choice.addItem("Apple");
choice.addItem("Banana");
choice.addItem("Cherry");
add(choice);
// create a text field
textField = new TextField(10); // 10 columns wide
add(textField);
// create a button
button = new Button("Click me");
add(button);
// create a label
label = new Label("I am a label");
add(label);
// create 3 exlusive-choice checkboxes
checkboxGroup = new CheckboxGroup();
checkbox = new Checkbox("Yes", checkboxGroup, false);
add(checkbox);
checkbox = new Checkbox("No", checkboxGroup, false);
add(checkbox);
checkbox = new Checkbox("Maybe", checkboxGroup, true);
add(checkbox);
}
/** Respond to user input events. */
public boolean action(Event e, Object arg) {
if (e.target == textField)
System.out.println("User entered text into the text field");
else if (e.target == button)
System.out.println("User clicked the button");
else if (e.target == choice)
System.out.println("User selected a new choice");
else if (e.target instanceof Checkbox)
System.out.println("User clicked a check box");
else
System.out.println("Unrecognized event");
return super.action(e, arg);
}
}